home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Internet Info 1994 March
/
Internet Info CD-ROM (Walnut Creek) (March 1994).iso
/
networking
/
ip
/
ka9q
/
src.arc
/
TTYDRIV.C
< prev
next >
Wrap
C/C++ Source or Header
|
1989-03-28
|
2KB
|
120 lines
/* TTY input driver */
#include <stdio.h>
#include "global.h"
#include "mbuf.h"
#include "tty.h"
static int edit;
static int echo;
#define OFF 0
#define ON 1
#define LINESIZE 256
#define CTLU 21
#define CTLR 18
#define CTLZ 26
/* Return current TTY status */
int
ttygetmode()
{
return (edit << 1) | echo;
}
/* Set TTY modes */
void
ttysetmode(x)
int x;
{
edit = (x & 2) ? 1 : 0;
echo = x & 1;
}
/* Accept characters from the incoming tty buffer and process them
* (if in cooked mode) or just pass them directly (if in raw mode).
* Returns the number of characters available for use; if non-zero,
* also stashes a pointer to the character(s) in the "buf" argument.
*/
/*Control-R added by df for retype of lines - useful in Telnet */
struct mbuf *
ttydriv(c)
char c;
{
static struct mbuf *bp;
struct mbuf *nbp;
static char *cp;
char *rp;
switch(edit){
case OFF:
if((nbp = alloc_mbuf(1)) == NULLBUF)
return NULLBUF;
*nbp->data = c;
nbp->cnt = 1;
if(echo)
putchar(c);
return nbp;
case ON:
if(bp == NULLBUF){
if((bp = alloc_mbuf(LINESIZE)) == NULLBUF)
return NULLBUF;
cp = bp->data;
}
/* Perform cooked-mode line editing */
switch(c & 0x7f){
case '\r': /* CR and LF are equivalent */
case '\n':
*cp++ = '\r';
*cp++ = '\n';
if(echo)
printf("\n");
bp->cnt = cp - bp->data;
nbp = bp;
bp = NULLBUF;
return nbp;
case '\b': /* Backspace */
if(cp != bp->data){
cp--;
if(echo)
printf("\b \b");
}
break;
case CTLR: /* print line buffer */
if(echo){
printf("^R\n") ;
rp = bp->data;
while (rp < cp)
putchar(*rp++) ;
}
break ;
case CTLU: /* Line kill */
while(cp != bp->data){
cp--;
if(echo){
printf("\b \b");
}
}
break;
default: /* Ordinary character */
*cp++ = c;
#ifndef AMIGA
/* ^Z apparently hangs the terminal emulators under
* DoubleDos and Desqview. I REALLY HATE having to patch
* around other people's bugs like this!!!
*/
if(echo && c != CTLZ && cp < &bp->data[LINESIZE-1])
putchar(c);
#endif
if(cp >= &bp->data[LINESIZE-1]){
putchar('\007'); /* Beep */
cp--;
}
break;
}
break;
}
return NULLBUF;
}